<?php

App::uses('CrudController', 'Controller');

class ManagersController extends CrudController
{
    public $_model = 'Manager';

    public function admin() {
        $this->request->allowMethod('get');
        $m = $this->_model;

        $appBosch = new stdClass();
        $appBosch->isRHManager = count($this->$m->haveRHManager());
        $this->set('appBosch', $appBosch);
        $this->set('title', __('Gerentes'));
        $this->set('description', 'Administración');
    }

    protected function getRecords($param) {
        $m = $this->_model;
        $records = $this->$m->getEnabled($param);
        return $records;
    }

    protected function c($model) {
        return array(
            'moe_id' => $model->moeId,
            'user_id' => $model->userId,
            'moeId' => $model->moeId,
            'userId' => $model->userId,
            'moeN' => $model->moeN,
            'userN' => $model->userN,
            'job' => $model->job,
            'type' => (int)$model->type,
            'signature' => (int)$model->sign,
        );
    }

    protected function id($model) {
        return $model->value;
    }

    protected function u($model) {
        return array(
            'value' => $model->value,
            'id' => $model->value,
            'moe_id' => $model->moeId,
            'user_id' => $model->userId,
            'moeId' => $model->moeId,
            'userId' => $model->userId,
            'moeN' => $model->moeN,
            'userN' => $model->userN,
            'job' => $model->job,
            'type' => (int)$model->type,
            'signature' => (int)$model->sign,
            'sign' => (int)$model->sign,
        );
    }

    public function uploadFiles() {
        $upload_dir = TMP;
        $path = $upload_dir . DS . 'ManagerSignatures';
        if (!file_exists($path)) {
            mkdir ($path, 0777, true);
        }
        $name = 'file.img';
        $file = $_FILES['sign'];
        move_uploaded_file($file['tmp_name'], $path . DS . $file['name']);
        $this->Session->write($name . '.name', $file['name']);
        $this->Session->write($name . '.path', $path);
        $succes = $path . DS . $file['name'];
        $this->set(array('succes' => $succes, '_serialize' => 'succes'));
        $this->viewClass = 'Json';
    }

    public function deletedUploaded() {
        $upload_dir = TMP;
        $path = $upload_dir . DS . 'ManagerSignatures';
        if($this->Session->check('file.img')) {
            $file = $this->Session->read('file.img');
            unlink($path . DS . $file['name']);
            $this->Session->delete("file.img");
        }
        $succes = $path . DS . $file['name'];
        $this->set(array('succes' => $succes, '_serialize' => 'succes'));
        $this->viewClass = 'Json';
    }

    protected function afterCreate(&$newRecord) {
        $newRecord['value'] = $newRecord['id'];

        $m = $this->_model;
        $this->$m->id = $newRecord['id'];
        $files = array();
        if($this->Session->check('file.img')) {
            $file = $this->Session->read('file.img');
            $filenameParts = explode('.', $file['name']);
            $ext = $filenameParts[1];
            $nn = uniqid("", true);
            $newName = $file['path'] . DS . $nn . '.' . $ext;
            rename($file['path'] . DS . $file['name'], $newName);
            $route = '/PHP_TEMP/ManagerSignatures/' . $nn . '.' . $ext;
            $files['signature'] = $route;
            $newRecord['sign'] = $route;
            $this->Session->delete("file.img");
            $this->$m->save($files);
        }
        return true;
    }

    protected function afterUpdate($id, &$updatedRecord) {
        $m = $this->_model;
        $this->$m->id = $id;
        $files = array();
        if($this->Session->check('file.img')) {
            $file = $this->Session->read('file.img');
            $filenameParts = explode('.', $file['name']);
            $ext = $filenameParts[1];
            $nn = uniqid("", true);
            $newName = $file['path'] . DS . $nn . '.' . $ext;
            rename($file['path'] . DS . $file['name'], $newName);
            $route = '/PHP_TEMP/ManagerSignatures/' . $nn . '.' . $ext;
            $files['signature'] = $route;
            $updatedRecord['sign'] = $route;
            $this->Session->delete("file.img");
            $this->$m->save($files);
        }
        return true;
    }

    protected function beforeCreate(&$newRecord)
    {
        if ($newRecord['type'] != 1) {
            $m = $this->_model;
            $res = $this->$m->findByStatusAndMoeIdAndType(1, $newRecord['moe_id'], $newRecord['type']);
            if (count($res)) {
                return false;
            }
        }

        return true;
    }

    protected function beforeUpdate($id, &$updatedModel)
    {
        if ($updatedModel['type'] != 1) {
            $m = $this->_model;
            $res = $this->$m->findByStatusAndMoeIdAndType(1, $updatedModel['moe_id'], $updatedModel['type']);
            if (count($res)) {
                return false;
            }
        }

        return true;
    }
}
